// 530. 二叉搜索树的最小绝对差
// https://leetcode.cn/problems/minimum-absolute-difference-in-bst/

import { TreeNode } from '../base/tree-node';

// 使用中序遍历实现和辅助数组实现
function getMinimumDifference(root: TreeNode | null): number {
  const arr: number[] = [];
  // 中序遍历
  function inorderTraversal(node: TreeNode | null) {
    if (!node) return;
    inorderTraversal(node.left);
    arr.push(node.val);
    inorderTraversal(node.right);
  }
  inorderTraversal(root);
  let minDiff = Number.MAX_SAFE_INTEGER;
  for (let i = 1; i < arr.length; i++) {
    minDiff = Math.min(minDiff, arr[i] - arr[i - 1]);
  }
  return minDiff;
}

export default getMinimumDifference;
